package com.project.service.impl;

import com.project.entity.po.Card;
import com.project.entity.qo.CardQo;
import com.project.exception.ServiceException;
import com.project.mapper.CardMapper;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.CardService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ProjectName:
 * @Package:
 * @ClassName:
 * @Author: ZhaoLongShi
 * @CreateDate:
 */
@SuppressWarnings("all")
@Service
public class CardServiceImpl implements CardService {
    @Resource
    private CardMapper cardMapper;

    /**
     * 查询所有会员类型
     *
     * @return
     */
    @Override
    public ResponseResult<List<Card>> selectAllCard() {
        List<Card> cards = cardMapper.selectAllCard();
        if (ObjectUtils.isEmpty(cards)) {
            throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
        }
        return ResponseResult.success(ResultCode.SUCCESS,cards);
    }

    /**
     * 添加会员类型
     *
     * @param cardQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> addCard(CardQo cardQo) {
        Long aLong = cardMapper.checkCard(cardQo.getCardName());
        if (aLong == 1) {
            throw new ServiceException(ResultCode.CARD_IS_EXISTENT);
        }
        Card card = new Card();
        BeanUtils.copyProperties(cardQo, card);
        int count = cardMapper.insert(card);
        if (count != 1) {
            throw new ServiceException(ResultCode.INSERT_FAILED);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 更新
     *
     * @param cardQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> updateCard(CardQo cardQo) {
        Card card = new Card();
        BeanUtils.copyProperties(cardQo, card);
        int count = cardMapper.updateById(card);
        if (count != 1) {
            throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 删除会员类型
     *
     * @param cardId
     * @return
     */
    @Override
    public ResponseResult<ResultCode> deleteCard(Long cardId) {
        int count = cardMapper.deleteCardById(cardId);
        if (count != 1) {
            throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }
}
